import numpy as np


# 创建函数，生成m个n维空间的点
def generator(n, m):
    X = np.array([])
    b = np.array([])
    for i in range(m):
        tmp = np.random.uniform(-10, 10, size=n - 1)
        tmp = np.append(tmp, 1)
        X = np.append(X, tmp)
        b = np.append(b, np.random.rand() * 100)
    X.shape = m, n
    b.shape = m, 1
    return X, b


# 创建10维空间上的1000个点
A, b = generator(10, 1000)

# 计算参数
A_T = A.T
A1 = np.dot(A_T, A)
A2 = np.linalg.inv(A1)
A3 = np.dot(A2, A_T)
X = np.dot(A3, b)
print(X)

# 平均误差
pred = np.dot(A, X)
V = np.sqrt(np.sum((pred - b) ** 2)) / 1000
print("拟合函数的平均误差为：", V)
